import { createRouter, createWebHashHistory } from 'vue-router'
import {message} from "ant-design-vue";
import {useUserStore} from "@/stores/index.js";

const routes = [
  {
    path: '/login',
    component: () => import('../views/check/LoginPage.vue')
  },
  {
    path: '/',
    component: () => import('../views/layout/IndexPage.vue'),
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('../views/layout/HomePage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/user',
        component: () => import('../views/layout/UserPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/emp',
        component: () => import('../views/layout/EmpPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/oplog',
        component: () => import('../views/layout/OplogPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/loginlog',
        component: () => import('../views/layout/LoginLogPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/pic',
        component: () => import('../views/layout/PicturePage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/online',
        component: () => import('../views/layout/OnlineUser.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/front',
        component: () => import('../views/layout/FrontPageManagement.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/data',
        component: () => import('../views/layout/DataStatistics.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/app',
        component: () => import('../views/layout/AppPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/question',
        component: () => import('../views/layout/QuestionPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/answer',
        component: () => import('../views/layout/AnswerPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/result',
        component: () => import('../views/layout/ResultPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/category',
        component: () => import('../views/layout/CategoryPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/question/add',
        component: () => import('../views/question/AddQuestionPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/question/edit/:id',
        component: () => import('../views/question/EditQuestionPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/browse',
        component: () => import('../views/layout/AppBrowsePage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/tokens',
        component: () => import('../views/layout/UserTokenPage.vue'),
        meta: {requiresAuth: true}
      },
      {
        path: '/suggest',
        component: () => import('../views/layout/UserSuggestPage.vue'),
        meta: {requiresAuth: true}
      }
    ]
  },
  {
    path: '/403',
    component: () => import('../views/error/403-page.vue'),
  },
  {
    path: '/404',
    component: () => import('../views/error/404-page.vue'),
  }
]

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: routes,
})

router.beforeEach((to, from, next) => {
  // 检查是否需要进行认证
  const userStore = useUserStore()

  // 如果路由不存在, 则跳转到 404 页面
  if (to.matched.length === 0) {
    return next('/404')
  }

  // 如果不需要认证, 则继续导航
  if (!to.matched.some(record => record.meta.requiresAuth)) {
    return next(); // 使用 return 确保只调用一次 next
  }

  // 检查是否存在 JWT 令牌
  if (!userStore.token) {
    message.error('请先登录')
    // 如果没有 JWT 令牌, 则跳转到登录页面
    return next('/login') // 使用 return 确保只调用一次 next
  }

  // 如果有 JWT 令牌, 则继续导航
  next() // 只有在上述条件都不满足时才会到达这里
})

export default router
